//
//  InsuranceInfoCell.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/7/12.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
class InsuranceInfoCell:UITableViewCell {
    var driveLicense_1,driveLicense_2,driveLicense_3,driveLicense_4,driveLicense_5,driveLicense_6,driveLicense_7,driveLicense_8,driveLicense_9,driveLicense_10,submitBtn:UIButton!
    var remark:UITextView!
    var okView,picView,remarkView:UIView!
    var money:UITextField!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.none
        self.layer.borderWidth = 0
    
        let iic = InsuranceInfoController()
        let label1 = UILabel.init(frame: CGRect(x: oLabelLeft, y: oTop, width: oLabelW, height: oLabelH))
        label1.font = oFontSize
        label1.text = "行驶证信息"
        self.addSubview(label1)
        //证件照片
        driveLicense_1 = UIButton.init(frame: CGRect(x: oImgLeft, y: oTop + oLabelH, width: oImgW, height: oImgH))
        driveLicense_1.setBackgroundImage(UIImage(named:"xingshizheng1"), for: .normal)
        self.addSubview(driveLicense_1)
        
        driveLicense_2 = UIButton.init(frame: CGRect(x: oImgLeft, y: oImgH + oLabelH + 2*oTop, width: oImgW, height: oImgH))
        driveLicense_2.setBackgroundImage(UIImage(named:"xingshizheng2"), for: .normal)
        self.addSubview(driveLicense_2)
        
        let label2 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 3*oTop + oLabelH + 2*oImgH, width: oLabelW, height: oLabelH))
        label2.font = oFontSize
        label2.text = "身份证信息"
        self.addSubview(label2)
        
        driveLicense_3 = UIButton.init(frame: CGRect(x: oImgLeft, y: 3*oTop + 2*oLabelH + 2*oImgH, width: oImgW, height: oImgH))
        driveLicense_3.setBackgroundImage(UIImage(named:"shenfenzheng1"), for: .normal)
        self.addSubview(driveLicense_3)
        
        driveLicense_4 = UIButton.init(frame: CGRect(x: oImgLeft, y: 4*oTop + 2*oLabelH + 3*oImgH, width: oImgW, height: oImgH))
        driveLicense_4.setBackgroundImage(UIImage(named:"shenfenzheng2"), for: .normal)
        self.addSubview(driveLicense_4)
        
        remarkView = UIView.init(frame: CGRect(x: 0, y: 5*oTop + 2*oLabelH + 4*oImgH, width: oLabelW, height: remarkH))
        self.addSubview(remarkView)
        
        let remarkLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y: 0, width: oLabelW, height: remarkH))
        remarkLabel.font = oFontSize
        remarkLabel.text = "备注信息"
        remarkView.addSubview(remarkLabel)
        
        remark = UITextView(frame:CGRect(x:oLabelW, y:0, width:oImgW, height:remarkH))
        remark.layer.cornerRadius = 5
        remark.backgroundColor = tableBgColor
        remark.font = oFontSize
        remark.textColor = fontColor
        remarkView.addSubview(remark)
        
        //服务费
        okView = UIView.init(frame:CGRect(x:0, y:6*oTop + 2*oLabelH + 4*oImgH + remarkH, width:kScreenW, height:cellH))
        okView.isHidden = true
        self.addSubview(okView)
        
        let moneyLabel = UILabel.init(frame: CGRect(x: oLabelLeft, y:0, width: oLabelW, height: cellH))
        moneyLabel.font = oFontSize
        moneyLabel.text = "服务费"
        okView.addSubview(moneyLabel)
        
        money = UITextField.init(frame: CGRect(x: oLabelW, y:0, width: oImgW, height: cellH))
        money.isEnabled = false
        money.backgroundColor = tableBgColor
        money.textColor = fontColor
        money.font = oFontSize
        money.layer.cornerRadius = 5
        okView.addSubview(money)
        
        //车身照
        picView = UIView.init(frame:CGRect(x:0, y:5*oTop + 2*oLabelH + 4*oImgH, width:kScreenW, height:6*oImgH + 6*oLabelH + 6*oTop))
        picView.isHidden = true
        //picView.backgroundColor = UIColor.red
        self.addSubview(picView)
        
        let label3 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 0, width: oLabelW, height: oLabelH))
        label3.font = oFontSize
        label3.text = "车身前左照"
        picView.addSubview(label3)
        
        driveLicense_5 = UIButton.init(frame: CGRect(x: oImgLeft, y: oLabelH, width: oImgW, height: oImgH))
        driveLicense_5.setBackgroundImage(UIImage(named:"front_left"), for: .normal)
        driveLicense_5.tag = 5
        driveLicense_5.addTarget(iic, action: #selector(iic.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        picView.addSubview(driveLicense_5)

        let label4 = UILabel.init(frame: CGRect(x: oLabelLeft, y: oTop + oLabelH + oImgH, width: oLabelW, height: oLabelH))
        label4.font = oFontSize
        label4.text = "车身前右照"
        picView.addSubview(label4)

        driveLicense_6 = UIButton.init(frame: CGRect(x: oImgLeft, y: oTop + 2*oLabelH + oImgH, width: oImgW, height: oImgH))
        driveLicense_6.setBackgroundImage(UIImage(named:"front_right"), for: .normal)
        driveLicense_6.tag = 6
        driveLicense_6.addTarget(iic, action: #selector(iic.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        picView.addSubview(driveLicense_6)

        let label5 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 2*oTop + 2*oLabelH + 2*oImgH, width: oLabelW, height: oLabelH))
        label5.font = oFontSize
        label5.text = "车身后左照"
        picView.addSubview(label5)
        
        driveLicense_7 = UIButton.init(frame: CGRect(x: oImgLeft, y: 2*oTop + 3*oLabelH + 2*oImgH, width: oImgW, height: oImgH))
        driveLicense_7.setBackgroundImage(UIImage(named:"after_left"), for: .normal)
        driveLicense_7.tag = 7
        driveLicense_7.addTarget(iic, action: #selector(iic.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        picView.addSubview(driveLicense_7)

        let label6 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 3*oTop + 3*oLabelH + 3*oImgH, width: oLabelW, height: oLabelH))
        label6.font = oFontSize
        label6.text = "车身后右照"
        picView.addSubview(label6)
        
        driveLicense_8 = UIButton.init(frame: CGRect(x: oImgLeft, y: 3*oTop + 4*oLabelH + 3*oImgH, width: oImgW, height: oImgH))
        driveLicense_8.setBackgroundImage(UIImage(named:"after_right"), for: .normal)
        driveLicense_8.tag = 8
        driveLicense_8.addTarget(iic, action: #selector(iic.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        picView.addSubview(driveLicense_8)

        let label7 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 4*oTop + 4*oLabelH + 4*oImgH, width: oLabelW, height: oLabelH))
        label7.font = oFontSize
        label7.text = "车前照"
        picView.addSubview(label7)
        
        driveLicense_9 = UIButton.init(frame: CGRect(x: oImgLeft, y: 4*oTop + 5*oLabelH + 4*oImgH, width: oImgW, height: oImgH))
        driveLicense_9.setBackgroundImage(UIImage(named:"front"), for: .normal)
        driveLicense_9.tag = 9
        driveLicense_9.addTarget(iic, action: #selector(iic.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        picView.addSubview(driveLicense_9)

        let label8 = UILabel.init(frame: CGRect(x: oLabelLeft, y: 5*oTop + 5*oLabelH + 5*oImgH, width: oLabelW, height: oLabelH))
        label8.font = oFontSize
        label8.text = "车后照"
        picView.addSubview(label8)
        
        driveLicense_10 = UIButton.init(frame: CGRect(x: oImgLeft, y: 5*oTop + 6*oLabelH + 5*oImgH, width: oImgW, height: oImgH))
        driveLicense_10.setBackgroundImage(UIImage(named:"behind"), for: .normal)
        driveLicense_10.tag = 10
        driveLicense_10.addTarget(iic, action: #selector(iic.choicePicture(sender:)), for: UIControlEvents.touchUpInside)
        picView.addSubview(driveLicense_10)
        
        submitBtn = UIButton.init(frame: CGRect(x: 20, y: 7*oTop + 2*oLabelH + 4*oImgH + remarkH, width: kScreenW - 40, height: cellH))
        submitBtn.setTitleColor(UIColor.white, for: .normal)
        submitBtn.backgroundColor = appMainColor
        submitBtn.showsTouchWhenHighlighted = true
        submitBtn.layer.cornerRadius = 10
        submitBtn.addTarget(iic, action: #selector(iic.operatorFun(sender:)), for: UIControlEvents.touchUpInside)
        self.addSubview(submitBtn)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func configCellDateWithModel(_ model:InsuranceModel) {
        driveLicense_1.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_1)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
        driveLicense_2.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_2)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
        driveLicense_3.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_3)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
        driveLicense_4.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_4)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
        remark.text = model.remark
        if model.isOriginals.isEmpty {
            submitBtn.setTitle("等待审核", for: .normal)
            submitBtn.isEnabled = false
            submitBtn.backgroundColor = UIColor.lightGray
        }else{
            okView.isHidden = false
            money.text = model.samount
            if "0" == model.isOriginals {//不需要车身照
                okView.frame.origin.y = 6*oTop + 2*oLabelH + 4*oImgH + remarkH
                submitBtn.frame.origin.y = 9*oTop + 2*oLabelH + 4*oImgH + remarkH + cellH
            }else{//需要车身照
                picView.isHidden = false
                okView.frame.origin.y = 12*oTop + 8*oLabelH + 10*oImgH
                remarkView.frame.origin.y = 13*oTop + 8*oLabelH + 10*oImgH + cellH
                submitBtn.frame.origin.y = 15*oTop + 8*oLabelH + 10*oImgH + cellH + remarkH
                if !model.driveLicense_5.isEmpty {
                    driveLicense_5.isEnabled = false
                    driveLicense_5.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_5)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
                }
                if !model.driveLicense_6.isEmpty {
                    driveLicense_6.isEnabled = false
                    driveLicense_6.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_6)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
                }
                if !model.driveLicense_7.isEmpty {
                    driveLicense_7.isEnabled = false
                    driveLicense_7.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_7)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
                }
                if !model.driveLicense_8.isEmpty {
                    driveLicense_8.isEnabled = false
                    driveLicense_8.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_8)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
                }
                if !model.driveLicense_9.isEmpty {
                    driveLicense_9.isEnabled = false
                    driveLicense_9.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_9)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
                }
                if !model.driveLicense_10.isEmpty {
                    driveLicense_10.isEnabled = false
                    driveLicense_10.sd_setBackgroundImage(with: URL.init(string: "\(projectUrl)/file/download/\(model.driveLicense_10)?tokenID=\(tokenID!)&tokenUserID=\(userID!)"), for: .normal)
                }
            }
            if "0" ==  model.ostatus{
                submitBtn.setTitle("去支付", for: .normal)
                submitBtn.tag = 0
            }
            if "1" == isEmployee {//普通用户登录
                if "1" == model.ostatus {
                    submitBtn.setTitle("等待处理", for: .normal)
                    submitBtn.isEnabled = false
                    submitBtn.backgroundColor = UIColor.lightGray
                }
                if "2" == model.ostatus {
                    submitBtn.setTitle("处理中", for: .normal)
                    submitBtn.backgroundColor = UIColor.lightGray
                    submitBtn.isEnabled = false
                }
                if "3" == model.ostatus {
                    submitBtn.setTitle("已处理", for: .normal)
                    submitBtn.isEnabled = false
                    submitBtn.backgroundColor = UIColor.lightGray
                }
            }
            if "2" == isEmployee {
                if "1" == model.ostatus {
                    submitBtn.setTitle("抢单", for: .normal)
                    submitBtn.tag = 1
                }
                if "2" == model.ostatus {
                    submitBtn.setTitle("完成", for: .normal)
                    submitBtn.tag = 2
                }
                if "3" == model.ostatus {
                    submitBtn.setTitle("已完成", for: .normal)
                    submitBtn.backgroundColor = UIColor.lightGray
                    submitBtn.isEnabled = false
                }
            }
        }
    }
}
